package LandR.personal.personalServlet;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import token.TokenHelp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("UpLoadServlet")
public class UpLoadServlet extends HttpServlet {

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=utf-8"); // 响应文本格式， json格式
        response.setCharacterEncoding("utf-8"); // 编码格式
        request.setCharacterEncoding("utf-8");

        if (request.getMethod().equals("PUT")) {
            /*
            token获取
             */
            String token = request.getHeader("token");

            token = token.substring(token.indexOf("\"") + 1, token.lastIndexOf("\""));

            System.out.println("前端传递的token令牌是：" + token);
            Integer ID = -1;
            try {
                TokenHelp th = new TokenHelp();
                ID = th.judgeToken(token);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            if (ID != -1) {
                String Path = request.getSession().getServletContext().getRealPath("/") + "WEB-INF\\images";
                System.out.println("文件上传的路径为：" + Path);
                String savePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF\\images2";
                System.out.println("文件保存的路径为：" + savePath);

                //判断文件路径是否存在
                File file1 = new File(Path);
                if (!file1.exists()) {
                    //创建上传文件夹
                    file1.mkdir();
                }
                File file2 = new File(savePath);
                if (!file2.exists()) {
                    //创建保存文件夹
                    file2.mkdir();
                }
                System.out.println("上传文件夹和保存文件夹创建成功");


                //使用smartUpload组件上传文件
                SmartUpload su = new SmartUpload();
                //初始化smartUpload
                su.initialize(getServletConfig(), request, response);
                //设置上传文件的大小 这里设置成10M
                su.setMaxFileSize(1024 * 1024 * 10);
                //设置上传所有文件的大小 这里设置成为100M
                su.setTotalMaxFileSize(1024 * 1024 * 100);
                //设置上传文件的类型
                su.setAllowedFilesList("txt,jpg,gif,png");
                //设置不允许上传文件的类型
                try {
                    su.setDeniedFilesList("rar,jsp,js");
                    //上传文件
                    su.upload();
                    com.jspsmart.upload.File fileE = su.getFiles().getFile(0);
                    String path = savePath + "\\" + ID + ".jpg";
                    fileE.saveAs(path);
                    System.out.println("上传成功");
                    this.getServletContext().setAttribute("path", path);
                    response.sendRedirect("UpdateServlet");
                } catch (SQLException | SmartUploadException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("失败！token已过期！");
                PrintWriter out = response.getWriter();
                out.print("失败！token已过期");
            }
        } else {
            PrintWriter out = response.getWriter();
            out.print("失败！提交方法不匹配或token不匹配！");
            System.out.println("失败！提交方法不匹配或token不匹配！");
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPut(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPut(req, resp);
    }
}
